package cn.edu.neusoft.dnui.startproject;

import android.app.ProgressDialog;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.dnui.startproject.factory.PhoneFactory;
import cn.edu.neusoft.dnui.startproject.listener.PhoneListener;
import cn.edu.neusoft.dnui.startproject.model.PhoneModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,PhoneListener {

    private EditText phoneView;
    private TextView msg_city;
    private ProgressDialog loadingCity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phoneView = (EditText)findViewById(R.id.phone_view);
        msg_city = (TextView)findViewById(R.id.msg_city);
        loadingCity = new ProgressDialog(this);
        loadingCity.setTitle("查询中...");
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        msg_city.setText("查询中...");
        loadingCity.show();
        switch(v.getId()){
            case R.id.button1:

                //工厂模式
                PhoneFactory.getInstance("baidu").getLocation(phoneView.getText().toString(), this);
                break;
            case R.id.button2:


                //工厂模式
                PhoneFactory.getInstance("91").getLocation(phoneView.getText().toString(), this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResponse(String city) {
        loadingCity.dismiss();
        if(city!=""&&city!=null)
            msg_city.setText(city);
        else
            msg_city.setText("未知");
    }

    @Override
    public void onFail(String msg) {
        loadingCity.dismiss();
        msg_city.setText("未知");
        //Toast.makeText(this,"fail:"+msg,Toast.LENGTH_SHORT).show();
    }
}
